// 导入PriorityFramework组件
import { Task } from '../../PriorityFramework/PriorityFramework';
import ButtonPlus from '../ButtonPlus';

/**
 * 短按任务类
 */
export class ShortClickTask implements Task {
    id: string;
    name: string;
    private _priority: number;
    private _button: ButtonPlus;
    private _event: cc.Event.EventTouch;

    constructor(button: ButtonPlus, event: cc.Event.EventTouch, priority: number) {
        this.id = 'short_click_task';
        this.name = 'Short Click Task';
        this._priority = priority;
        this._button = button;
        this._event = event;
    }

    getPriority(): number {
        return this._priority;
    }

    setPriority(priority: number): void {
        this._priority = priority;
    }

    execute(): void {
        // 短按功能现在负责触发标准的点击事件，无论长按是否开启
        // 这确保了优先级系统可以正确控制所有事件的触发
        cc.Component.EventHandler.emitEvents(this._button.clickEvents, this._event);
        const shortClickData = { target: this._button, event: this._event };
        this._button.node.emit('shortclick', shortClickData);
        this._button['_logEvent']('shortclick', shortClickData);
    }
}

/**
 * 长按任务类
 */
export class LongClickTask implements Task {
    id: string;
    name: string;
    private _priority: number;
    private _button: ButtonPlus;
    private _event: cc.Event.EventTouch;

    constructor(button: ButtonPlus, event: cc.Event.EventTouch, priority: number) {
        this.id = 'long_click_task';
        this.name = 'Long Click Task';
        this._priority = priority;
        this._button = button;
        this._event = event;
    }

    getPriority(): number {
        return this._priority;
    }

    setPriority(priority: number): void {
        this._priority = priority;
    }

    execute(): void {
        if (this._button['_state'].longFlag) {
            const longEndData = { target: this._button, event: this._event };
            this._button.node.emit('longend', longEndData);
            this._button['_logEvent']('longend', longEndData);
            this._button['_state'].longFlag = false;
        }
    }
}

/**
 * 连点任务类
 */
export class MultiClickTask implements Task {
    id: string;
    name: string;
    private _priority: number;
    private _button: ButtonPlus;
    private _event: cc.Event.EventTouch;

    constructor(button: ButtonPlus, event: cc.Event.EventTouch, priority: number) {
        this.id = 'multi_click_task';
        this.name = 'Multi Click Task';
        this._priority = priority;
        this._button = button;
        this._event = event;
    }

    getPriority(): number {
        return this._priority;
    }

    setPriority(priority: number): void {
        this._priority = priority;
    }

    execute(): void {
        // 检查点击计数
        if (this._button['_state'].clickCount === this._button.multiCount) {
            // 使用统一格式的事件数据
            const multiData = { target: this._button, event: this._event, count: this._button.multiCount };
            this._button.node.emit('multi', multiData);
            this._button['_logEvent']('multi', multiData);
            cc.Component.EventHandler.emitEvents(this._button.clickEvents, this._event);

            // 修复：延迟重置计数器，允许连续触发多次连点事件
            this._button.scheduleOnce(() => {
                if (this._button['_isValidComponent']()) {
                    this._button['_state'].clickCount = 0;
                }
            }, 0.1); // 100ms后重置，允许快速连续触发
        } else if (this._button['_state'].clickCount > 1) {
            // 设置重置定时器（使用命名方法）
            this._button.scheduleOnce(this._button['_resetClickCount'], this._button.multiInterval);
        }
    }
}

/**
 * 滑动任务类
 */
export class SlideTask implements Task {
    id: string;
    name: string;
    private _priority: number;
    private _button: ButtonPlus;
    private _event: cc.Event.EventTouch;

    constructor(button: ButtonPlus, event: cc.Event.EventTouch, priority: number) {
        this.id = 'slide_task';
        this.name = 'Slide Task';
        this._priority = priority;
        this._button = button;
        this._event = event;
    }

    getPriority(): number {
        return this._priority;
    }

    setPriority(priority: number): void {
        this._priority = priority;
    }

    execute(): void {
        const state = this._button['_state'];
        if (state.isSliding) {
            const slideEndData = { target: this._button, event: this._event, distance: state.currentSlideDistance };
            this._button.node.emit('slideend', slideEndData);
            this._button['_logEvent']('slideend', slideEndData);
        }
    }
}